package com.bl.car.controller;

import com.bl.car.constants.StateConstant;
import com.bl.car.entity.Owner;
import com.bl.car.entity.User;
import com.bl.car.repositiy.DriverRepository;
import com.bl.car.repositiy.OwnerRepository;
import com.bl.car.repositiy.UserRepository;
import com.bl.car.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 业户对外接口
 *
 * @author zhx
 * @create 2017-11-16 11:33
 **/

@RestController
@RequestMapping("/owner")
public class OwnerController {
    @Autowired
    private OwnerRepository ownerRepository;

    @Autowired
    private UserRepository userRepository;



    //获取要注销的驾驶员
    @PostMapping("/delowner")
    public Result<List<Owner>> delowner(){
        List<Owner> byDriverId = ownerRepository.findByIsEffect(StateConstant.DELETE_TYPE);

        return Result.ok(byDriverId);

    }


    //获取列表已经审核通过的业户列表
    @PostMapping("/getAllOwners")
    public Result<List<Owner>> getAllOwners(){
        List<User> byIsEffect = userRepository.findByIsEffect(StateConstant.USER_OWNER);

        if(byIsEffect.size()==0){
            return Result.ok(null);
        }

        List<String> tempList = new ArrayList<>();

        for(User u:byIsEffect){
            tempList.add(Long.toString(u.getId()));
        }

        List<Owner> byUserIds = ownerRepository.findByUserIds(tempList);

        return Result.ok(byUserIds);
    }

    //获取业主详情
    @PostMapping("/getOwnerDetailByUser/{id}")
    public Result<Owner> getOwnerDetail(@PathVariable("id") String userId){
        Owner byUserId = ownerRepository.findByUserId(userId);

        return Result.ok(byUserId);
    }

    @PostMapping("/saveOwner/{id}")
    public Result<Owner> saveOwner(@RequestBody Owner owner){
        Owner save = ownerRepository.save(owner);

        return Result.ok(save);
    }

}
